﻿using NB.MGJ.Common.MVC;
using NB.MGJ.Model;
using NB.MGJ.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NB.MGJ.Web.Controllers
{
    public partial class CodeManageController : BaseController
    {
      

        private FleeGoodsService _mservice = new FleeGoodsService();
        public ActionResult CodeRelateList()
        {

            return View();
        }

        public ActionResult CodeRelateAdd()
        {
            ViewData["ProductId"] = _productService.GetProductSelectList(0, LogOnUser.EnterpriseId);

            ViewData["ProductBatchId"] = _ProductBatchService.GetBatchSelectList(0, LogOnUser.EnterpriseId);

            ViewData["CodeBatch"] = _CodeRelateService.GetGenCodeBatchList(LogOnUser.EnterpriseId, 0);

            return View();
        }

        public ActionResult GetCodeRelatePageList(int pageIndex = 1, int pageSize = 10, int action = 0)
        {

            PagerModel pm = new PagerModel();
            pm.Page = pageIndex;
            pm.PageSize = pageSize;
            var pageList = _CodeRelateService.GetCodeRelatePageList(ref pm, LogOnUser, action);
            return Json(pageList, JsonRequestBehavior.AllowGet);
           
        }

      
        public ActionResult CodeRelateSave(CodeManage_CodeRelate model,string BoxCodes)
        {
            var result = true;
            model.ChangeLoginID = LogOnUser.UserID;
            model.ChangeDate = DateTime.Now;
            model.EnterpriseId = LogOnUser.EnterpriseId;
            if (model.RelateType == 2 && BoxCodes != null) { 
            BoxCodes = BoxCodes.Replace("\t", "");
            BoxCodes = BoxCodes.Replace("\n", "");
            BoxCodes = BoxCodes.Replace("\r", "");
            BoxCodes = BoxCodes.Replace("，", ",");
            string[] arr = BoxCodes.TrimEnd(',').Split(',');

            string tableName = string.Empty;
            for (int i = 0; i < arr.Length; i++)
            {
                if (string.IsNullOrWhiteSpace(arr[i].Trim()))
                {
                    return Json("第" + i + "个码后面存在多余的字符！");
                }

                Organization_Enterprise ent = _mservice.GetMaxEnterpriseIInfo(LogOnUser.EnterpriseId);//获取最上级的机构信息
                if (arr[i].Trim().Substring(0, 4) != ent.Code)
                {
                    return Json("第" + (i + 1) + "个码不属于当前企业！"); ;
                }
                AntiFleeing_ProductReturn model0 = _mservice.GetModel<AntiFleeing_ProductReturn>(arr[i]);
                if (model0 == null) return Json("第" + (i + 1) + "个码不存在！");

                var list = _codeManageService.ListGetTableStats(arr[i]);
              
                //判断是否冻结
                if (list[0].Stats != 0)
                    return Json("第" + (i + 1) + "个码已禁用！");

            }
            result = _codeManageService.disablewlcode(model, BoxCodes.TrimEnd(','), 1);//禁用
            
            if (!result)
            {
                return Json("请检查该批次码是否被禁用或码数据是否存在！");
            }
            return Json(true);
            }
           
          
            if (model.Action == 1)
            {
                result = _codeManageService.CodeDisableAdd(model, LogOnUser.EnterpriseId);
            }
            else
            {
                result = _codeManageService.CodeRelate(model);
            }
            //return Json(result, JsonRequestBehavior.AllowGet);
            if (!result)
            {
                return Json("请检查该批次码是否被禁用或码数据是否存在！");
            }
            return Json(true);
        }
        //码禁用
        public int CodeDisableflag(int id, int scode, int ecode, string batch, int type, string wl_codedisable,int action)
        {
            int result = _codeManageService.CodeDisableflag(scode, ecode, LogOnUser.ECode, id, batch, type, wl_codedisable, action); 
            return result;
        }


    }
}